<?php
include("_session.php");
if(count($_POST)>0)
{
	$order_no = $_POST['ORDNO'];
	$ordcamph = $_POST["ORDCAMP"];		
	$datetime = explode(" ", $_POST['ORDDATE']);
	$orddate = wrapperDate($datetime[0]);
	$ordtime = str_replace(":","",$datetime[1]);
	$curcamp = getCurCamp($conn);
	$dist = str_pad($_POST["DIST"],4,0,STR_PAD_LEFT);
	$mslno = str_pad($_POST["MSLNO"],5,0,STR_PAD_LEFT);
	$chkdgt = $_POST["CHKDGT"];
	$memberno = $_POST["MEMBERNO"];
	$membername = $_POST["MSLNAME"];
	$totalamount = 0;
	$delflag = "N";
	$dwnflag = "N";	

	if (HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt,$upddate,$updtime))
	{
		$respond_message = "CONCURRENCE";
		$error_message = "CONCURRENCE! ".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
	}
	else
	{
        mysql_query("BEGIN");        
		$sql = "DELETE FROM order_detail WHERE 
				ORDER_NO = ".$order_no." AND
				ORDCAMPH = ".$ordcamph." AND 
				DIST = '".$dist."' AND
				MSLNO = ".$mslno." AND 
				CHKDGT = ".$chkdgt." AND 				
				DWNFLAG = '".$dwnflag."' AND 
				DELFLAG = '".$delflag."'";
				//echo $sql;
    	$result = mysql_query($sql, $conn);
    	if (!$result)
    	{
    		$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
    		error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");
			mysql_query("ROLLBACK");	    		
    		$respond_message = "ERROR";
			$error_message = "ROLLBACK! D".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
			//echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
		}
		else
		{
			$rows = $_SESSION['ORDER_DETAIL'];
			$i=0;
			foreach($rows AS $rowNum => $row)
			{
		        $LISTNO = ++$i;
		        $BILLCODE = $row['BILLCODE'];
		        $BILLDESC = $row['BILLDESC'];
		        $QTY = str_replace(",","", $row['QTY']);        
		        $PRICE = str_replace(",","", $row['PRICE']);
		        $AMOUNT = str_replace(",","", $row['AMOUNT']);
		        $ORDCAMPD = $row['ORDCAMPD'];
		        $REMARK = 'N';
		        $BRAND = 'N';
		        $DISCOUNT = 'NULL';
		        $SPCFLG = 'N';
		        $DISCFLG = 'N';
		        $INCTFLG = 'N';
		        $FREEFLG = 'N';
		        $BILLFLG = 'N';
		        $DWNFLG = 'N';
		        $EXPFLG = 'N';
		        $FLAG1 = 'N';
		        $DELFLAG = 'N';
		        
		        $sql = "INSERT INTO order_detail (
		        	ORDER_NO ,
					ORDCAMPH ,
					ORDDATE ,
					ORDTIME ,
					CURCAMP ,
					DIST ,
					MSLNO ,
					CHKDGT ,
					LISTNO ,
					BILLCODE ,
					BILLDESC ,
					QTY ,
					PRICE ,
					AMOUNT ,
					REMARK ,
					BRAND ,
					DISCOUNT ,
					SPCFLG ,
					DISCFLG ,
					INCTFLG ,
					FREEFLG ,
					BILLFLAG ,
					DWNFLAG ,
					EXPFLAG ,
					FLAG1 ,
					DELFLAG ,
					ORDCAMPD
		        ) VALUES (
		        	$order_no,  
		        	$ordcamph,  
		        	$orddate,  
		        	'".$ordtime."',  
		        	$curcamp,  
		        	'".$dist."',  
		        	$mslno,  
		        	$chkdgt,  
		        	$LISTNO,  
		        	'".$BILLCODE."',  
		        	'".$BILLDESC."',  
		        	$QTY,  
		        	$PRICE,  
		        	$AMOUNT,  
		        	'".$REMARK."', 
		        	'".$BRAND."' ,  
		        	$DISCOUNT,  
		        	'".$SPCFLG."',  
		        	'".$DISCFLG."',  
		        	'".$INCTFLG."',  
		        	'".$FREEFLG."',  
		        	'".$BILLFLG."',  
		        	'".$DWNFLG."',  
		        	'".$EXPFLG."',  
		        	'".$FLAG1."',  
		        	'".$DELFLAG."',  
		        	$ORDCAMPD
		        )";
		        //echo $sql;
		    	$result = mysql_query($sql, $conn);
		    	if (!$result)
		    	{
		    		$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
		    		error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");
					mysql_query("ROLLBACK");	    		
		    		$respond_message = "ERROR";
					$error_message = "ROLLBACK! D".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
					//echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
					break;
				}
				$totalamount = $totalamount + $AMOUNT;
			}			
			if ($result)
			{
				$sql = "UPDATE order_header 
				SET 
					ITEMS = $i,
					TOTAL_AMOUNT = $totalamount, 
					ACCOUNT_ID = '".$_SESSION["RACCOUNT_ID"]."',
					UPDDATE = '".date("Ymd")."',
					UPDTIME = '".date("His")."' 
				WHERE 
					ORDER_NO = ".$order_no." AND
					ORDCAMP = ".$ordcamph." AND 
					DIST = '".$dist."' AND
					MSLNO = ".$mslno." AND 
					CHKDGT = ".$chkdgt." AND 				
					DWNFLAG = '".$dwnflag."' AND 
					DELFLAG = '".$delflag."'";
				//echo $sql;
				$result = mysql_query($sql, $conn);
		    	if (!$result)
		    	{
		    		$log = date("H:i:s")."-".$_SESSION["RACCOUNT_ID"];
		    		error_log($log.": ".mysql_error($conn).PHP_EOL, 3, "D:/AppServ/www/tosorderbackup/log/err".date("Ymd").".log");
					mysql_query("ROLLBACK");	    		
		    		$respond_message = "ERROR";
					$error_message = "ROLLBACK! H".$order_no."/".$ordcamph."(".$dist."-".$mslno."-".$chkdgt.")";
					//echo mysql_errno($conn) . ": " . mysql_error($conn). "\n";
				}
				else
				{
					mysql_query("COMMIT");
					$respond_message = "SUCCESSFULL";
				}
			}			
		}			
	}
	header("Location:resultupdate.php?respond_message=".$respond_message."&dist=".$dist."&mslno=".$mslno."&chkdgt=".$chkdgt."&ordcamp=".$ordcamph."&order_no=".$order_no);
}

function HeaderExists($conn,$order_no,$ordcamph,$dist,$mslno,$chkdgt,$upddate,$updtime)
{
	$sql = "SELECT count(*) as numrow 
	FROM order_header oh 
		WHERE 
		oh.DIST ='".$dist."'  
	AND oh.MSLNO = ".$mslno." 
	AND oh.CHKDGT = ".$chkdgt." 
	AND oh.ORDCAMP = ".$ordcamph." 
	AND oh.ORDER_NO = ".$order_no."
	AND oh.UPDDATE = '".$upddate."'  
	AND oh.UPDTIME = '".$updtime."'";
	//echo $sql;
	$raw = mysql_query($sql, $conn);
	$row = mysql_fetch_array($raw, MYSQL_ASSOC);
	if ($row["numrow"] > 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}
include("_close.php");
?>